/**   
 * Model Created on 2022年5月30日
 *   
 * Copyright (c) 2018 by Kunrong.HOo.
 */ 
package com.seaside.park.api;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seaside.common.bean.Result;
import com.seaside.park.service.ReserveOrderService;
import com.seaside.parkCommon.bean.ReserveOrder;
import com.seaside.parkCommon.bean.User;
import com.seaside.parkCommon.mapper.ParkPlaceMapper;
import com.seaside.parkCommon.mapper.UserMapper;
import com.seaside.parkCommon.vo.ParkPlaceVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 
 * @author <a href="mailto:hoocoral@hotmail.com">Kunrong.HOo</a>
 * @version 1.0
 */
@Api(tags="告警展示")
@RestController
@RequestMapping("/api/search")
public class SearchAPI {
	
	@Autowired
	private ReserveOrderService reserveOrderService;
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private ParkPlaceMapper parkPlaceMapper;
	
    @ApiOperation(value = "根据车牌号查询预约电话", response = Result.class)
    @ApiImplicitParams({
    	@ApiImplicitParam(paramType="query",name="carPlate",dataTypeClass=String.class,required=true,value="车牌号")
    })
    @PostMapping("/reserve/mobile")
    public Result<List<String>> queryReserveOrders(@RequestParam String carPlate) {
    	
    	if (StringUtils.isBlank(carPlate)) {
    		return Result.Error("请输入车牌号！");
    	}
    	
    	return reserveOrderService.getReserveMobiles(carPlate);
    }
    
    @ApiOperation(value = "查询登录人的分页订单列表", response = Result.class)
    @ApiImplicitParams({
    	@ApiImplicitParam(paramType="query",name="curpage",dataTypeClass=Integer.class,required=true,value="当前页")
    })
    @PostMapping("/reserve/pageByMobile")
    public Result<IPage<ReserveOrder>> queryReserveOrdersPageByMobile(@ApiIgnore Authentication auth, @RequestParam Integer curpage) {
        
    	if (auth == null) {
    		return Result.Error("未登录！");
    	}
    	
        User user = userMapper.selectById(auth.getName());
        if (user == null) {
    		return Result.Error("未找到用户信息！");
        }
    	
    	Integer pageSize = 10;
    	if (curpage == null) {
    		curpage = 1;
    	}
    	Page<ReserveOrder> page = new Page<>(curpage, pageSize, false);
    	
    	return reserveOrderService.getOrderPage(page, user.getUserTel(), null);
    }
    
    @ApiOperation(value = "查询登录人的分页订单列表", response = Result.class)
    @ApiImplicitParams({
    	@ApiImplicitParam(paramType="query",name="curpage",dataTypeClass=Integer.class,required=true,value="当前页")
    })
    @PostMapping("/reserve/pageByParkNo")
    public Result<IPage<ReserveOrder>> queryReserveOrdersPageByParkNo(@ApiIgnore Authentication auth, @RequestParam Integer curpage) {
        
    	if (auth == null) {
    		return Result.Error("未登录！");
    	}
    	
        User user = userMapper.selectById(auth.getName());
        if (user == null) {
    		return Result.Error("未找到用户信息！");
        }
    	
        Integer pageSize = 10;
    	if (curpage == null) {
    		curpage = 1;
    	}
    	Page<ReserveOrder> page = new Page<>(curpage, pageSize, false);
        
        List<ParkPlaceVO> parkPlaceList = parkPlaceMapper.getParkPlaceByUserName(user.getUserName());
        if (!CollectionUtils.isEmpty(parkPlaceList)) {
        	List<Integer> parkPlaceIdList = parkPlaceList.stream().map(ParkPlaceVO::getParkId).collect(Collectors.toList());
        	
        	return reserveOrderService.getOrderPage(page, null, parkPlaceIdList);
        }
        
    	return Result.Error("查询错误！");
    	
    }

}